import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
//引入axios
import axios from "axios";
//配置axios全局基本路径
axios.defaults.baseURL="http://localhost:8080"
//拓展属性
Vue.prototype.$http=axios
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
//1.axios前置拦截器，作用：所有ajax请求携带token给后端
axios.interceptors.request.use(config=>{
  //携带token
  let uToken =  localStorage.getItem("token");
  if(uToken){
    config.headers['U-TOKEN']=uToken;
  }
  return config;
},error => {
  Promise.reject(error);
});
// 3. 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
  let data = result.data;
  console.log("======================")
  console.log(result)
  console.log("======================")
  if(!data.success && data.resultObj==="noLogin"){
    //清除浏览器的历史数据
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    router.push({ path: '/echarts' });//登录成功，跳转到主页
  }
  if(!data.success && data.resultObj==="noPermission"){
    router.push({ path: '/noPermission' });//跳转到没有权限的页面
  }
  return result;
},error => {
  Promise.reject("前端出错了");
})
//4.前端静态资源拦截器
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == '/shopRegister') {
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    next();//放行路由
  }else{//你访问的是非静态资源，我们需要校验是否登录
    let user = localStorage.getItem('logininfo');
    if(user){
      next();//放行路由
    }else{
      next({ path: '/login' });//阻止，定位到登录页
    }
  }
});
// 注册一个全局自定义指令 `v-perm`
Vue.directive('perm', {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: (el, binding, vnode) => {
    const value = binding.value;//department:deleteById
    //拿出当前登录人所拥有的按钮权限
    let permissions = localStorage.getItem('permissions');
    if (permissions){
      let auths = JSON.parse(permissions);
      //权限不为空，且当前要渲染的按钮没有包含在权限中，那么我就移除按钮
      if (auths && auths.join(",").indexOf(value)==-1) {
        el.parentNode.removeChild(el);
      }
    }
  }
});
//========================我们自定义的拦截器
// router.beforeEach((to, from, next) => {
//   //NProgress.start();
//   if (to.path == '/login') {
//     sessionStorage.removeItem('user');
//   }
//   let user = JSON.parse(sessionStorage.getItem('user'));
//   if (!user && to.path != '/login') {
//     next({ path: '/login' })
//   } else {
//     next()
//   }
// })

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

